//
//  QuarterPickupPopView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/20.
//

import Foundation
import UIKit
import JFPopup

protocol QuarterPickupPopViewDelegate: AnyObject {
    func handleConfirm(_ year: Int, quarter: Int)
}

class QuarterPickupPopView: UIView {
    
    let datas = [1, 2, 3 , 4]
    
    var selectQuarter = 1 {
        didSet {
            q_picker.selectRow(datas[selectQuarter - 1] - 1, inComponent: 0, animated: false)
        }
    }
    
    
    var selYear: Int = 0 {
        didSet {
            y_picker.date = Calendar.current.date(from: DateComponents(year: selYear)) ?? Date()
        }
    }
    
    weak var delegate: QuarterPickupPopViewDelegate?
   
    let titleL = UILabel()
    let y_picker = BSDatePicker()
    let q_picker = UIPickerView()
    
    let saveBtn = UIButton.createPrimaryLarge("确定")

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func closeToggle() {
        (self.getFirstViewController() as! JFPopupController).dismiss(animated: true)
    }
    
    @objc private func confirmToggle() {
        (self.getFirstViewController() as! JFPopupController).dismiss(animated: true)
        
        let quarter = datas[q_picker.selectedRow(inComponent: 0)]
        self.delegate?.handleConfirm(y_picker.selectedDateComponent.year, quarter: quarter)

    }
    
    func setupUI() {
        let closeBtn = UIButton(type: .custom)
        closeBtn.setTitle("✕", for: .normal)
        closeBtn.setTitleColor(.black, for: .normal)
        closeBtn.setTitleColor(.gray, for: .highlighted)
        closeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 26)
        closeBtn.addTarget(self, action: #selector(closeToggle), for: .touchUpInside)
        addSubview(closeBtn)
        closeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.width.height.equalTo(48)
        }
        
        titleL.text = "选择日期"
        titleL.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        titleL.textColor = .black
        addSubview(titleL)
        titleL.snp.makeConstraints { (make) -> Void in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.snp.top).offset(20)
        }
        
        y_picker.dateFormatType = .year
        y_picker.minimumDate = Date().addingRmoving(years: -10)
        y_picker.maximumDate = Date()
        addSubview(y_picker)
        y_picker.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left).offset(30)
            make.top.equalTo(titleL.snp.bottom).offset(16)
            make.width.equalTo((ScreenWidth - 80) / 2.0)
            make.height.equalTo(200)
        }
        
        q_picker.dataSource = self
        q_picker.delegate = self
        q_picker.selectRow(datas[selectQuarter - 1] - 1, inComponent: 0, animated: false)
        addSubview(q_picker)
        q_picker.snp.makeConstraints { make in
            make.left.equalTo(y_picker.snp.right).offset(20)
            make.right.equalTo(self.snp.right).offset(-30)
            make.top.equalTo(y_picker.snp.top)
            make.width.equalTo((ScreenWidth - 80) / 2.0)
            make.height.equalTo(200)
        }
        
        saveBtn.addTarget(self, action: #selector(confirmToggle), for: .touchUpInside)
        addSubview(saveBtn)
        saveBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(y_picker.snp.bottom).offset(36)
            make.left.equalTo(self.snp.left).offset(32)
            make.right.equalTo(self.snp.right).offset(-32)
            make.height.equalTo(50)
        }
    }
}

extension QuarterPickupPopView: UIPickerViewDataSource, UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return datas.count
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel()
        label.textAlignment = .center
        let attri: [NSAttributedString.Key : Any] = [.font : UIFont.systemFont(ofSize: 16)]
        let attriStr = NSMutableAttributedString(string:  "第\(datas[row])季度", attributes: attri)
        label.attributedText = attriStr
        return label
    }
}
